<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Usersinvitations extends Model
{
    protected $table = 'users_invitations';
    protected $primaryKey = 'id';

    public static function getagentOrders($users_id)
    {
        return self::select('users.nike','users.avatar','orders_member.order_no','orders_member.name','orders_member.product_id','orders_member.price','orders_member.pay_status','orders_member.start_time','orders_member.end_time','orders_member.created_at')
            ->leftjoin('users', 'users.id', '=', 'orders_member.users_id')
            ->leftjoin('orders_member', 'orders_member.users_id', '=', 'users_invitations.users_id')
            ->where("users_invitations.users_invitation_id", $users_id)
            ->orderBy('orders_member.id', 'desc')
            ->paginate(25);
    }

    public static function adminGetUsersInvitations($users_ids)
    {
        return self::select('users.username','users.nike','users_invitations.users_id','users_invitations.users_invitation_id')
            ->leftjoin('users', 'users.id', '=', 'users_invitations.users_invitation_id')
            ->whereIn("users_invitations.users_id", $users_ids)
            ->get();
    }

    public static function checkAgentUsers($users_invitation_id,$users_id)
    {
        return self::where("users_id", $users_id)->where("users_invitation_id", $users_invitation_id)->first();
    }

    public static function getUsersInvitation($users_id)
    {
        return self::where("users_id", $users_id)->first();
    }

    public static function addUsersInvitation($users_id,$users_invitation_id)
    {
        return self::insert(["users_id"=>$users_id,"users_invitation_id"=>$users_invitation_id,"created_at"=>time()]);
    }

    public static function getAgentUsersLists($users_id,$nike,$limit=25)
    {
        $query = self::select('users.*')->leftjoin('users', 'users.id', '=', 'users_invitations.users_id')->where("users_invitations.users_invitation_id", $users_id);
        if (!empty($nike)) $query->where("users.nike","like","%".$nike."%");
        return $query->orderBy('users_invitations.id', 'desc')->paginate($limit);
    }
}
